package com.devframe.srtp.server.file;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.devframe.srtp.Account;
import com.devframe.srtp.SRTPConstants;
import com.devframe.srtp.SRTPResult;
import com.devframe.srtp.server.parse.SRTPParam;
import com.devframe.srtp.util.enumobj.EnumSRTPAction;

public class SRTPDeleteFileHandler {

	private static final Log log=LogFactory.getLog(SRTPDeleteFileHandler.class);
	
	private Account account;
	
	
	public SRTPDeleteFileHandler(Account account){
		this.account=account;
		//路径保存到根路径下
	}
	public SRTPResult process(SRTPParam reqParams){
		SRTPResult result=new SRTPResult();
		result.setCode(false);
		result.setAction(EnumSRTPAction.DELETE_FILE.getValue());
		
		if(StringUtils.isNotBlank(reqParams.getFilePath())){
			//真实路径
			String realPath=this.account.getRootPath()+reqParams.getFilePath();
			if(log.isDebugEnabled()){
				log.debug("进行删除文件："+realPath);
			}
			File file=new File(realPath);
			//必须是文件的
			boolean bool=false;
			if(file.exists() && file.isFile()){
				bool=file.delete();
			}
			//进行判断是否有缩略图
			int position = realPath.lastIndexOf(".");
			String suffix = realPath.substring(position);
			String thumbPath=realPath.substring(0,position)+SRTPConstants.THUMB_SUFFIX+suffix;
			//进行删除缩略图操作
			File thumbFile=new File(thumbPath);
			//必须是文件的
			if(thumbFile.exists() && thumbFile.isFile() && bool){
				thumbFile.delete();
			}
			result.setCode(bool);
			result.setMsg("文件删除成功");
		}
		if(!result.isCode()){
			result.setMsg("文件删除失败");
		}
		return  result;
	}
}
